package com.ruoyi.common.core.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysDictTree;
import com.ruoyi.common.core.mapper.SysDictTreeMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 知识点分类实现类
 */
@Service
public class SysDictTreeServiceImpl extends BaseServiceImpl<SysDictTreeMapper, SysDictTree> {

    /**知识点保存*/
    public void doSave(SysDictTree vo) {
       if(vo.getPid() == null){
          vo.setPid(0L);
       }
       vo.insertOrUpdate();
       List<SysDictTree> child =  baseMapper.selectByPid(vo.getPid());
       if(vo.getPid().longValue() == 0){
          vo.setFullId(","+vo.getId()+",");
       }else{
           SysDictTree parent = baseMapper.selectById(vo.getPid());
           vo.setFullId(parent.getFullId()+vo.getId()+",");
       }
       if(vo.getSort() == null){
           vo.setSort(child.size());
       }
        vo.updateById();
    }

    public void doDelete(Long id) {
        //1、检查是否存在知识点

        List<SysDictTree> child = baseMapper.selectByPid(id);
        Assert.isFalse(child.size() > 0,"请先删除子节点");

        //2、删除
        this.removeById(id);
    }
}
